﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QuanLiSinhVien_v01.DTO;

namespace QuanLiSinhVien_v01.DAO
{
    class HocKyDao: DataProvider
    {
        public HocKyDto HocKy { get; set; }

        protected override Object GetDataFromDataRow(DataTable dt, int rowIndex)
        {
            HocKyDto HocKyDto = new HocKyDto();
            HocKyDto.MaHocKy = dt.Rows[rowIndex]["MaHocKy"].ToString();
            HocKyDto.TenHocKy = dt.Rows[rowIndex]["TenHocKy"].ToString();
            HocKyDto.MaKhoaHoc = dt.Rows[rowIndex]["MaKhoaHoc"].ToString();
            HocKyDto.GhiChu = dt.Rows[rowIndex]["GhiChu"].ToString();

            return HocKyDto;
        }

        public HocKyDto[] GetList()
        {
            string strCommand = " Select * from HocKy ";
            DataTable dt = ExcuteQuery(strCommand);
            int n = dt.Rows.Count;
            HocKyDto[] kq = new HocKyDto[n];
            for (int i = 0; i < n; i++)
            {
                kq[i] = (HocKyDto)GetDataFromDataRow(dt, i);
            }
            return kq;
        }

        public void Insert()
        {
            string strCommand =
                "insert into HocKy (MaHocKy, TenHocKy, MaKhoaHoc , GhiChu) " +
                "values ('" + HocKy.MaHocKy + "', '" +
                              HocKy.TenHocKy + "', '" +
                              HocKy.MaKhoaHoc + "', " +
                              HocKy.GhiChu + "'" +
                        ")";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update()
        {
            string strCommand =
                "update HocKy set " +
                "TenHocKy = '" + HocKy.TenHocKy + "' , " +
                "MaKhoaHoc = '" + HocKy.MaKhoaHoc + "' , " +
                "GhiChu = '" + HocKy.GhiChu + "' " +
                "where MaHocKy = '" + HocKy.MaHocKy + "' ";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete()
        {
            string strCommand =
                "DELETE FROM HocKy " +
                "WHERE  MaHocKy = '" + HocKy.MaHocKy + "'";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
